<style type="text/css">
    #hr-examineSimple-update {
        padding: 20px 25px 25px 0;
    }
</style>

<!-- 注意id属性每个页面id必须独立-->
<div class="layui-fluid" id="hr-examineSimple-update">
    <!-- 注意lay-filter属性，该属性为form表单唯一属性-->
    <form class="layui-form" action="" lay-filter="hr-examineSimple-update-form">
        <!-- 隐藏当前对象id-->
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">试卷id：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${examineSimple.id}" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">试卷名称</label>
            <div class="layui-input-block">
                <input type="text" name="title" minlength="2" maxlength="100" lay-verify="required|range|txt"
                       autocomplete="off" class="layui-input repetitive" id="hr-examineSimple-add-title"
                >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">考试时长</label>
            <div class="layui-input-block">
                <input type="text" name="minutes" maxlength="11" lay-verify="range|txt|char|number"
                       autocomplete="off" class="layui-input repetitive" >
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="examineSimple-update-form-submit"
                    id="hr-examineSimple-update-submit-btn"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'validate'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            // 获得后台出入的实体对象
            examineSimple = [[${examineSimple}]],
            // 验证
            validate = layui.validate;
        // 表单验证
        form.verify(validate);
        // 表单渲染
        form.render();
        // 初始化页面数据
        initExamineSimpleValue();
        // 初始化页面数据
        function initExamineSimpleValue() {
            form.val("hr-examineSimple-update-form", {
                "title": examineSimple.title,
                "minutes": examineSimple.minutes
            });
        }

        // 表单提交事件
        form.on('submit(examineSimple-update-form-submit)', function (data) {
             console.log("进入修改" + data.field)
            // 如果都没有修改直接返回
            if (febs.nativeEqual(data.field, examineSimple)) {
                febs.alert.warn('数据未作任何修改！');
                return false;
            // 如果只修改了名字之外的东西，则直接更新
            }else if(febs.nativeEqual(data.field.title, examineSimple.title)){
                febs.post(ctx + 'examineSimple/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('试卷数据修改成功');
                    // 点击父页面查询按钮
                    $('#hr-examineSimple').find('#hr-examineSimple-query-btn').click();
                });
            // 如果名字发生变化先进行判断是否重名，如果重名则返回，不重名则修改
            }else {
                let title = data.field.title;
                if(febs.nativeEqual(title, examineSimple.title)){
                    title = null;
                }
                $.ajax({
                    type: 'get',
                    url: 'examineSimple/isRepetitive',
                    data: {"title": title},
                    success:function(res){
                        if(res){
                            febs.post(ctx + 'examineSimple/update', data.field, function () {
                                layer.closeAll();
                                febs.alert.success('试卷数据修改成功');
                                $('#hr-examineSimple').find('#hr-examineSimple-query-btn').click();
                            });
                        }else{
                            febs.alert.error("输入的试卷名称重复，请检查！");
                        }
                    }
                });
            }
            return false;
        });
    });
</script>